package com.moorecat.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class StringUtils {
	/**
	 * 字符串摘要
	 * @param s
	 * @return
	 */
	public static String md5(String s) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        String eText = null;
        if (s != null && !"".equals(s)) {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(s.getBytes("UTF-8"));
            byte[] digest = md.digest();
            eText = byte2Hex(digest);
        }
        return eText;
    }
    /**
     * 二进制byte转成十六进制
     *
     * @param bytes
     * @return
     */
    public static String byte2Hex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        if (bytes != null) {
            for (byte b : bytes) {
                sb.append(String.format("%02x", b));
            }
        }
        return sb.toString();
    }
}
